package com.mao.crowd.mvc.intercepter;

import com.mao.crowd.constant.CrowdConstant;
import com.mao.crowd.exception.AccessDeninedException;
import entity.Admin;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author: Administrator
 * @description:
 * @cteate 2021-05-28-20:23
 */
public class LoginIntercepter extends HandlerInterceptorAdapter {
//继承HandlerInterceptorAdapter,重写preHandle方法

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        通过request对象获取session
        HttpSession session = request.getSession();

//        尝试从session域中获取admin对象
        Admin admin = (Admin) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN);
//        判断admin是否为空
        if (admin == null){
            throw new AccessDeninedException(CrowdConstant.MESSAGE_ACCESS_FORBIDDEN);
        }
//        如果Admin对象不为null,则放行
        return true;
    }
}
